package com.chenyuhan.myview.viewpager.clipViewPager;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by Chenyuhan on 2017/9/5.
 */

public class ViewPagerImageViewAdapter extends PagerAdapter {

    private List<ImageView> imageViewList;

    public ViewPagerImageViewAdapter(List<ImageView> imageViews) {
        this.imageViewList = imageViews;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= imageViewList.size();
        if (position < 0) {
            position = imageViewList.size() + position;
        }
        ImageView view = imageViewList.get(position);
        if (view.getParent() != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            parent.removeView(view);
        }
        container.addView(view);
        return view;
    }
}
